class Solution {
    public int minDistance(String word1, String word2) {
      int s1=word1.length()+1;
      int s2=word2.length()+1;
      int [][] dp=new int[s1][s2];
      for(int i=0;i<=s1-1;i++){
          dp[i][0]=i;
      }
      for(int j=0;j<=s2-1;j++){
          dp[0][j]=j;
      }
       for(int i=1;i<=s1-1;i++){
           for(int j=1;j<=s2-1;j++){
               if(word1.charAt(i-1)==word2.charAt(j-1)){
                dp[i][j]=dp[i-1][j-1];
               }else{
                   dp[i][j]=Math.min(dp[i-1][j]+2,Math.min(dp[i-1][j],dp[i][j-1])+1);
               }
           }
       }

       return dp[s1-1][s2-1];
    }
}